<?php

namespace backend\controllers;

use Yii;
use common\models\MktFriend;
use common\models\MktFriendSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * FriendController implements the CRUD actions for MktFriend model.
 */
class FriendController extends Controller
{
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all MktFriend models.
     * @return mixed
     */
    public function actionIndex($user_id)
    {
        Yii::$app->request->queryParams = yii\helpers\ArrayHelper::merge(Yii::$app->request->queryParams,
            [ 'MktFriendSearch' => ['user_id' => $user_id]]
        );     
        $model = \common\models\MktUser::findOne($user_id);            
    
        $searchModel = new MktFriendSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'model' => $model,
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single MktFriend model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new MktFriend model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate($user_id)
    {
        $model = new MktFriend();
        $model->loadDefaultValues();                
        $model->user_id = $user_id;

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
                $model_x = new MktFriend();
                $model_x->loadDefaultValues();                
                $model_x->user_id = $model->friend_id;
                $model_x->friend_id = $model->user_id;                
                $model_x->save(false); 
	         return $this->redirect(['index', 'user_id'=>$model->user_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
        
    }

    /**
     * Updates an existing MktFriend model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing MktFriend model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete();
        $model_x = MktFriend::findOne(['user_id' => $model->friend_id, 'friend_id' => $model->user_id]);
        if (null !== $model_x) {
            $model_x->delete();
        }
        return $this->redirect(['index', 'user_id' => $model->user_id]);
    }

    /**
     * Finds the MktFriend model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return MktFriend the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MktFriend::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
